package com.ruoyi.yuangong.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yuangong.domain.Yuangong;
import com.ruoyi.yuangong.service.IYuangongService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 上班人数管理Controller
 * 
 * @author ruoyi
 * @date 2023-01-31
 */
@Controller
@RequestMapping("/yuangong/yuangongTest")
public class YuangongController extends BaseController
{
    private String prefix = "yuangong/yuangongTest";

    @Autowired
    private IYuangongService yuangongService;

    @RequiresPermissions("yuangong:yuangongTest:view")
    @GetMapping()
    public String yuangongTest()
    {
        return prefix + "/yuangongTest";
    }

    /**
     * 查询上班人数管理列表
     */
    @RequiresPermissions("yuangong:yuangongTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, Yuangong yuangong)
    {
//        startPage();
//        List<Yuangong> list = yuangongService.selectYuangongList(yuangong);
//        return getDataTable(list);
        startPage();
        String a = request.getSession().getAttribute("QuanXian").toString();
        if (a.equals("0") && (yuangong == null || yuangong.getDianming().equals(""))) {
            List<Yuangong> namee2cangku = yuangongService.selectYuangongList(yuangong);
            Yuangong gouhuoRuku1 = new Yuangong();
            for (Yuangong c:namee2cangku) {
                if (c.getDianming().equals(request.getSession().getAttribute("name4user").toString())) {
                    gouhuoRuku1.setDianming(c.getDianming());
                }
            }
            List<Yuangong> list = yuangongService.selectYuangongList(gouhuoRuku1);
            return getDataTable(list);
        }else {
            startPage();
            List<Yuangong> list = yuangongService.selectYuangongList(yuangong);

            return getDataTable(list);
    }
    }

    /**
     * 导出上班人数管理列表
     */
    @RequiresPermissions("yuangong:yuangongTest:export")
    @Log(title = "上班人数管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Yuangong yuangong)
    {
        List<Yuangong> list = yuangongService.selectYuangongList(yuangong);
        ExcelUtil<Yuangong> util = new ExcelUtil<Yuangong>(Yuangong.class);
        return util.exportExcel(list, "上班人数管理数据");
    }

    /**
     * 新增上班人数管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存上班人数管理
     */
    @RequiresPermissions("yuangong:yuangongTest:add")
    @Log(title = "上班人数管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Yuangong yuangong)
    {
        return toAjax(yuangongService.insertYuangong(yuangong));
    }

    /**
     * 修改上班人数管理
     */
    @RequiresPermissions("yuangong:yuangongTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Yuangong yuangong = yuangongService.selectYuangongById(id);
        mmap.put("yuangong", yuangong);
        return prefix + "/edit";
    }

    /**
     * 修改保存上班人数管理
     */
    @RequiresPermissions("yuangong:yuangongTest:edit")
    @Log(title = "上班人数管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Yuangong yuangong)
    {
        return toAjax(yuangongService.updateYuangong(yuangong));
    }

    /**
     * 删除上班人数管理
     */
    @RequiresPermissions("yuangong:yuangongTest:remove")
    @Log(title = "上班人数管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yuangongService.deleteYuangongByIds(ids));
    }
}
